// import 'quadcopter.dart';
import 'dart:json' as json;
//import 'dart:math' as math;
 
int theNextVersion = 1;
int getNextVersion() { return theNextVersion++; }
   
 
 // Does all the work 
 // can serialize and (I hope) run in
 // server and Browser
class QuadCopterManager {
   int version;
   Map copters;
  
  // current position 
  // different on each call
  void updateCopter(QuadCopter added)
  {
    copters[added.id] = added;
    version = getNextVersion();
  }
  
  // Named constructor - takes object name
  QuadCopterManager(  )   
  {
    version = getNextVersion();
    copters = new Map();
   }
  
 
    
  Map toJson() {
    return {
      "version": version,
      "copters": json.stringify(copters),
     };
  }


  // Equals does not work
  // this tolerates roundoff
  bool isEquivalent(QuadCopterManager o)
  {
    if(version != o.version)
      return false;
    if(copters.length != o.copters.length)
      return false;
    for(String id in copters.keys) {
       if(!copters[id].isEquivalent(o.copters[id]))
         return false;
    }
       
     return true;
  }
  
  // Named constructor
  // 
  QuadCopterManager.fromJson(Map mp) {
    version = mp['version'];
    copters = json.parse( mp['copters']);
  }
  
}